/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAL.ConnectUnit;
import DAO.DataAccessObject;
import DTO.CustomerDTO;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author Mr.Sam
 */
public class CustomerBUS {
    public ConnectUnit conn;
    public CustomerBUS()
    {
        conn=DAL.DAL.getDAL();
    }
    //insert
    public boolean Insert(String procedure,CustomerDTO info) throws Exception
    {
        CallableStatement cs=conn.GetCallableStatement(procedure);
        cs.setString(1, info.getCustomerID());
        cs.setString(2, info.getFullName());
        cs.setString(3, info.getIDNumber());
        cs.setString(4, info.getHomeAddress());
        cs.setString(5, info.getEmail());
        cs.setString(6, info.getPhone());
        return cs.execute();
        //return conn.Insert("Customer", customer);
    }
    public boolean Update(String procedure,CustomerDTO info) throws Exception
    {
        CallableStatement cs=conn.GetCallableStatement(procedure);
        cs.setString(1, info.getCustomerID());
        cs.setString(2, info.getFullName());
        cs.setString(3, info.getIDNumber());
        cs.setString(4, info.getHomeAddress());
        cs.setString(5, info.getEmail());
        cs.setString(6, info.getPhone());
        return cs.execute();
        //return conn.Insert("Customer", customer);
    }
    public boolean Delete(String procedure,CustomerDTO info) throws Exception
    {
        CallableStatement cs=this.conn.GetCallableStatement(procedure);
        cs.setString(1,info.getCustomerID());
        cs.registerOutParameter(2, java.sql.Types.BOOLEAN);
        cs.execute();
        boolean result=cs.getBoolean(2);
        if(result==false)
        return true;
        else
        return false;
    }
    public ResultSet ExcuteQuery(String procedure,String sql) throws Exception
    {
        CallableStatement cs=this.conn.GetCallableStatement(procedure);
        cs.setString(1, sql);
        return cs.executeQuery();
    }
    //kiem tra thon tin khach hang co ton tai tren he thong chua
    //neu ton tai thi tra ve true
    public boolean KiemTraThongTin_KH(String procedure,String HoTen,String CMND) throws Exception
    {
        CallableStatement cs=this.conn.GetCallableStatement(procedure);
        cs.setString(1,HoTen);
        cs.setString(2,CMND);
        cs.registerOutParameter(3, java.sql.Types.BOOLEAN);
        cs.execute();
        boolean result=cs.getBoolean(3);
        if(result==true)
            return true;
        else
            return false;
        
    }
    public String Search(
            boolean bCustomerID,String CustomerID,
            boolean bFullName,String FullName,
            boolean bIDNumber,String IDNumber,
            boolean bHomeAddress,String HomeAddress,
            boolean bEmail,String Email,
            boolean bPhone,String Phone
            )
    {
        boolean isFirst=true;
        String sql="select * from customer ";
        if(bCustomerID)
        {
            sql+="where CustomerID like '%"+CustomerID+"%' ";
            isFirst=false;
        }
        //
        if(bFullName)
        {
             if (isFirst)
                {
                    sql += "where FullName like '%" + FullName + "%' ";
                    isFirst = false;
                }
             else
             {
                 sql+="and FullName like '%" + FullName + "%' ";
             }
        }
        //
         if(bIDNumber)
        {
             if (isFirst)
                {
                    sql += "where IDNumber like '%" + IDNumber + "%' ";
                    isFirst = false;
                }
             else
             {
                 sql+="and IDNumber like '%" + IDNumber + "%' ";
             }
        }
         // 
        if(bHomeAddress)
        {
             if (isFirst)
                {
                    sql += "where HomeAddress like '%" + HomeAddress + "%' ";
                    isFirst = false;
                }
             else
             {
                 sql+="and HomeAddress like '%" + HomeAddress + "%' ";
             }
        }
        //
        if(bEmail)
        {
             if (isFirst)
                {
                    sql += "where Email like '%" + Email + "%' ";
                    isFirst = false;
                }
             else
             {
                 sql+="and Email like '%" + Email + "%' ";
             }
        }
        //
        if(bPhone)
        {
             if (isFirst)
                {
                    sql += "where Phone like '%" + Phone + "%' ";
                    isFirst = false;
                }
             else
             {
                 sql+="and Phone like '%" + Phone + "%' ";
             }
        }
        return sql;
    }
    public static void main(String[] str) throws Exception
    {
      CustomerBUS bus=new CustomerBUS();
      JOptionPane.showMessageDialog(null,bus.KiemTraThongTin_KH("{call Check_Customer(?,?,?)}", "do ngoc sam", "123"));
    } 
}
